import { request, USE_MOCK } from './request'

export type Product = {
  id: string
  storeName: string
  name: string
  category: string
  image?: string
  specType: '单规格' | '多规格'
  price: number
  stock: number
  storeAddress?: string
  storePhone?: string
}

const mockProducts: Product[] = [
  {
    id: '1',
    storeName: '海淀门店',
    name: '白衬衫',
    category: '上衣',
    image: 'https://via.placeholder.com/120',
    specType: '单规格',
    price: 120,
    stock: 100
  },
  {
    id: '2',
    storeName: '朝阳门店',
    name: '牛仔裤',
    category: '裤装',
    specType: '多规格',
    price: 220,
    stock: 60
  }
]

export async function getProducts(): Promise<Product[]> {
  return request({ url: '/products', method: 'get' }, mockProducts)
}

export async function addProduct(product: Omit<Product, 'id'>) {
  if (USE_MOCK) {
    const newProduct: Product = { id: Date.now().toString(), ...product }
    mockProducts.push(newProduct)
    return request({}, { code: 0, message: '新增成功 (mock)' })
  }
  return request({ url: '/products', method: 'post', data: product })
}

export async function deleteProduct(id: string) {
  if (USE_MOCK) {
    const idx = mockProducts.findIndex(p => p.id === id)
    if (idx !== -1) mockProducts.splice(idx, 1)
    return request({}, { code: 0, message: '删除成功 (mock)' })
  }
  return request({ url: `/products/${id}`, method: 'delete' })
}
